//
//  YCContentPageView.m
//  TitleContent
//
//  Created by 唐可雄 on 2019/7/19.
//  Copyright © 2019 swustyc. All rights reserved.
//

#import "YCContentPageView.h"

#import <Tool/Tool.h>

@interface YCContentPageView ()<UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate> {
    NSMutableDictionary *_contentVCs;
    NSUInteger _selectedIndex;
    NSUInteger _number;
}

@property (nonatomic, strong) UIPageViewController *pageVC;

@end

@implementation YCContentPageView

#pragma mark - Super-Sub View

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
    
    if (newSuperview) {
        [self configViewWithViewController:[newSuperview currentViewController]];
    }
}

#pragma mark - View Config

- (void)configViewWithViewController:(UIViewController *)vc {
    self.translatesAutoresizingMaskIntoConstraints = NO;
    _number = [self.dataSource numberOfPagesInContentView:self];
    _contentVCs = [NSMutableDictionary dictionary];
    
    self.pageVC = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    self.pageVC.dataSource = self;
    self.pageVC.delegate = self;
    self.pageVC.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self.pageVC setViewControllers:@[[self contentViewControllerAtIndex:_selectedIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    if ([self.pageVC.view.subviews.firstObject isKindOfClass:[UIScrollView class]]) {
        ((UIScrollView *)self.pageVC.view.subviews.firstObject).delegate = self;
    }
    [self addSubview:self.pageVC.view];
    [vc addChildViewController:self.pageVC];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.pageVC.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.pageVC.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.pageVC.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.pageVC.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
}

#pragma mark - YCContentPageView

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    
    if (_contentVCs) {
        [self.pageVC setViewControllers:@[[self contentViewControllerAtIndex:_selectedIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    }
}

#pragma mark - UIPageViewControllerDelegate

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {

}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    UIViewController *curVC = pageViewController.viewControllers.firstObject;
    for (NSNumber *index in _contentVCs) {
        if (_contentVCs[index] == curVC) {
            if (_selectedIndex != [index integerValue]) {
                _selectedIndex = [index integerValue];
                [self.delegate contentView:self didSelectAtIndex:_selectedIndex];
            }
        }
    }
}

#pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if (_selectedIndex > 0) {
        return [self contentViewControllerAtIndex:_selectedIndex - 1];
    }
    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if (_selectedIndex < _number - 1) {
        return [self contentViewControllerAtIndex:_selectedIndex + 1];
    }
    return nil;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat distance = scrollView.contentOffset.x - scrollView.bounds.size.width;
    if (distance == 0) {
        
    } else if (distance < 0) {
        if (_selectedIndex > 0) {
            [self.delegate contentView:self transitionToTargetIndex:_selectedIndex - 1 progress:-distance / scrollView.bounds.size.width];
        }
    } else if (distance > 0) {
        if (_selectedIndex < _number - 1) {
            [self.delegate contentView:self transitionToTargetIndex:_selectedIndex + 1 progress:distance / scrollView.bounds.size.width];
        }
    }
}

#pragma mark - Load

- (void)reloadData {
    _number = [self.dataSource numberOfPagesInContentView:self];
    _contentVCs = [NSMutableDictionary dictionary];
}

#pragma mark - Other

- (UIViewController *)contentViewControllerAtIndex:(NSUInteger)index {
    if (!_contentVCs[@(index)]) {
        _contentVCs[@(index)] = [self.dataSource contentView:self viewControllerAtIndex:index];
    }
    return _contentVCs[@(index)];
}

@end
